package cn.tedu.adminsystem.webapi.service.impl;

import cn.tedu.adminsystem.service.PhotoUploadService;
import cn.tedu.community.commons.excepion.ServiceException;
import cn.tedu.community.commons.restful.ServiceCode;
import cn.tedu.community.commons.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
@Slf4j
public class PhotoUploadImpl implements PhotoUploadService {

    @Override
    public String getPicFileName(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        log.debug("图片上传Service层开始处理图片上传，文件原始名字是：{}",oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf('.'));
        String filePath = "D:\\Vue-Workspace\\Community-Web\\src\\views\\img\\admin\\";
        String newFileName = System.currentTimeMillis() + typeName;
        try {
            FileUtil.uploadFile(file.getBytes(), filePath, newFileName); //第三步，合成新的uuid值
        } catch (Exception e) {
            throw new RuntimeException();
        }
        return newFileName;
    }

    @Override
    public void deletePhoto(String uuid) {
        String filePath = "D:\\Vue-Workspace\\Community-Web\\src\\views\\img\\admin\\"+uuid;
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
    }
}
